/**
 * small and fast plug to load the scripts 
 * which have dependencies asynchronously
 * 8/26 修改成根据索引Index来定义和加载script
 */
var script = {};
(function(load){
	var models = {};
	//a map dictionary records the 'script' request
	var reqWorks = {};
	var storeMap = {};
	var host = window.location.host;//get the default host
	var path = function(){//get the default contentPath(默认第一个为容器地址)
		var pathname = window.location.pathname;
		var reg = new RegExp("^/\\w*");
		return reg.exec(pathname)+"/";
	}();
	load.path = function(npath){
		if(typeof(npath) != 'undefined'){
			path = npath+"/";
		}
	};
	load.host = function(nhost){
		if(typeof(nhost) != 'undefined'){
			host = nhost+"/";
		}
	};
	load.map = function(map){
		if(map instanceof Object || map instanceof Array){
			for(var key in map){
				storeMap[key] = map[key];
			}
		}else{
			throw new Error("illegal scripts' map !");
		}
	};
	load.define = function(idx,dependentIdxes,callback){
		var model = {};
		var url = null;
		if(storeMap.length<=0||typeof(url=storeMap[idx])=='undefined'){
			throw new Error("can't not find the url by the idx : "+idx);
		}
		if(null == url || url == ''){
			throw new Error("'url' indexed by "+idx+" can't not be empty");
		}
		if(null == models[idx]){
			model.url = url;
			if(null != dependentIdxes){
				if(dependentIdxes instanceof Array){
					model.dependentIdxes = dependentIdxes;
					for(var array in dependentIdxes){
						var dependentIdx = dependentIdxes[array];
						if(typeof(models[dependentIdx])=="undefined"){
							throw new Error("can't not find the dependency named "+key);
						}else{
							models[dependentIdx].parents.push(idx);
						}
					}
				}else{
					throw new Error("the dependent indexes:"+dependentIdxes+" should be a Array!");
				}
			}
			model.parents = [];
			model.callback = callback;
			model.isLoad = false;
			model.idx = idx;
			models[idx]=model;
		}
	};
	load.load = function(idx){
		var model = {};
		if(typeof(model = models[idx])=="undefined"){
			throw new Error("can't load the script whose index is "+idx);
		}
		var reqWork = {};
		reqWork.url = model.url;
		reqWork.dependentIdxes = model.dependentIdxes;
		reqWork.parents = model.parents;
		reqWork.complete = false;
		reqWorks[idx]=reqWork;
		prepareLoad(model);
	};
	function prepareLoad(model){
		var flag = true;
		if(null != model.dependentIdxes){
			var dependentIdxes = model.dependentIdxes;
			for(var array in dependentIdxes){
				var dependentIdx = dependentIdxes[array];
				if(typeof(reqWorks[dependentIdx])=='undefined'){
					flag = false;
					load.load(dependentIndex);
				}else if(!reqWorks[dependentIdx].complete){
					flag = false;
				}
			}
		}
		if(flag){
			load0(model);
		}
	};
	function load0(model){
		if(!model.isLoad){
			model.isLoad = true;
			var head = document.head||document.getElementsByTagName("head")[0];
			var script0 = document.createElement("script");
			script0.type = "text/javascript";
			if(script0.readyState){
				script0.onreadystatechange = function(){
					if(script0.readyState=="loaded"||script0.readyState=="complete"){
						if(null != model.callback){
							model.callback();
						}
						updateReqWork(model.idx);
					};
				};
			}else{
				script0.onload=function(){
					if(null != model.callback){
						model.callback();
					}
					updateReqWork(model.idx);
				};
			}
			script0.src = "http://"+window.location.host+path+model.url;//组合请求
			head.appendChild(script0);
		}
	};
	function updateReqWork(idx){
		var reqWork = reqWorks[idx];
		reqWork.complete = true;
		var parentIdxs = reqWork.parents;
		for(var i in parentIdxs){
			var parentIdx = parentIdxs[i];
			var parentWork = reqWorks[parentIdx];
			if(typeof(parentWork) == "undefined"){
				continue;
			}
			var siblingIdxes = parentWork.dependentIdxes;
			var flag = 1;
			for(var j = 0;j<siblingIdxes.length;j++){
				if(!reqWorks[siblingIdxes[j]].complete){
					flag = 0;
					break;
				}
			}
			if(flag == 1){
				load0(models[parentIdx]);
			}
		}
	};
	function host(){
		var host0 = window.location.host;
		//will be?
		if(host0 == null){
			host0 = document.domain;
		}
		return host0;
	};
})(script);
